﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;
using System.Windows.Resources;

namespace Microsoft.Telco.Controls.WindowsPhone
{
    /// <summary>
    /// Type containing helper methods for controls
    /// </summary>
    public static class ControlHelper
    {
        /// <summary>
        /// /// Key: popup id, Value: Popup instance
        /// </summary>
        public static Dictionary<string, Popup> PopupTracker = new Dictionary<string,Popup>();

        /// <summary>
        /// Gets or sets a value indicating whether this instance is win phone7.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is win phone7; otherwise, <c>false</c>.
        /// </value>
        public static bool IsWindowsPhone
        {
            get
            {
                return System.Environment.OSVersion.Platform == PlatformID.WinCE;
            }
            private set { }
        }

        /// <summary>
        /// Hide All POPUPS 
        /// </summary>
        public static void HideAllPopups()
        {
            //can't use VisualTreeHelper.GetOpenPopups() -- doesnt work for WP7, runtime check doesn't help
            var popups = PopupTracker.Values;
            foreach (var popup in popups)
            {
                if (popup.IsOpen)
                {
                    popup.IsOpen = false;
                }
            }
        }

        /// <summary>
        /// Gets the image from resource.
        /// </summary>
        /// <param name="relativePath">The relative path.</param>
        /// <returns></returns>
        public static BitmapImage GetImageFromResource(string relativePath)
        {
            string assemblyShortName = Assembly.GetExecutingAssembly().FullName.Split(new char[] { ',' })[0];
            string path = string.Format("{0};component/{1}", assemblyShortName, relativePath);
            StreamResourceInfo sr = Application.GetResourceStream(new Uri(path, UriKind.Relative));
            BitmapImage image = new BitmapImage();
            image.SetSource(sr.Stream);
            return image;
        }
    }
}
